package com.dunqi.gpm.cangxi.home.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;

import com.dunqi.gpm.cangxi.R;
import com.dunqi.gpm.cangxi.common.api.ApiFactory;
import com.dunqi.gpm.cangxi.common.api.RxSchedulers;
import com.dunqi.gpm.cangxi.common.base.BaseAct;
import com.dunqi.gpm.cangxi.common.base.ComObserver;
import com.dunqi.gpm.cangxi.common.base.Data;
import com.dunqi.gpm.cangxi.common.view.ComTitleBar;
import com.dunqi.gpm.cangxi.home.bean.Project;
import com.dunqi.gpm.cangxi.home.constance.Constance;
import com.dunqi.gpm.cangxi.home.eventbus.FinishAndRefresh;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

public class ProjectBaseAutidActivity extends BaseAct {


    @BindView(R.id.title_bar)
    ComTitleBar titleBar;
    @BindView(R.id.pass_rb)
    RadioButton passRb;
    @BindView(R.id.not_pass_rb)
    RadioButton notPassRb;
    @BindView(R.id.audit_des_edit)
    EditText auditDesEdit;
    private ProgressDialog dialog;
    private Project project;
    private int checked;

    @Override
    protected void refreshData(Bundle savedInstanceState) {

    }

    @Override
    protected void initUiAndListener() {

        project = (Project) getIntent().getSerializableExtra(Constance.PROJECT);
        checked = getIntent().getIntExtra(Constance.CHECKED, 1);
        if (checked == 1) {
            passRb.performClick();
        } else {
            notPassRb.performClick();
        }

        titleBar.setTitle(project.getProjectName());

        titleBar.setOnClickListenerOfRight(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (dialog == null) {
                    dialog = ProgressDialog.show(getActivity(), "提示", "正在提交");
                } else {
                    dialog.show();
                }
                Observable<Data> observable;
                if (passRb.isChecked()) {
                    observable = ApiFactory.getInstance().accept(project.getProjectId(), getContent());
                } else {
                    observable = ApiFactory.getInstance().reject(project.getProjectId(), getContent());
                }
                observable.compose(RxSchedulers.<Data>io_main())
                        .subscribe(new ComObserver() {
                            @Override
                            public void onSubscribe(Disposable d) {
                                addDisposables(d);
                            }

                            @Override
                            public void onSuccess(Object o) {
                                setResult(100, new Intent());
                                EventBus.getDefault().postSticky(new FinishAndRefresh());
                                finish();
                                t("提交成功");
                            }

                            @Override
                            public void onError(Throwable e) {
                                super.onError(e);
                                t("提交失败");
                            }

                            @Override
                            public void onFinal() {
                                dialog.dismiss();
                            }
                        });


            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_project_autid;
    }


    public String getContent() {
        return auditDesEdit.getText().toString().trim();
    }

    /**
     * @param context
     * @param checked 选择 1  不选0
     */
    public static void start(Activity context, Project project, int checked, int requestCode) {
        Intent intent = new Intent(context, ProjectBaseAutidActivity.class);
        intent.putExtra(Constance.PROJECT, project);
        intent.putExtra(Constance.CHECKED, checked);
        context.startActivityForResult(intent, requestCode);
    }

}
